package com.terrynoya.fight.stateMachine.controllers
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.char.Entity;
	import com.terrynoya.fight.combat.HitDefinition;
	import com.terrynoya.fight.emuns.AttackEffect;
	import com.terrynoya.fight.emuns.HitAnimationType;
	import com.terrynoya.fight.evaluation.ExpressionRunner;
	import com.terrynoya.fight.io.TextSection;
	import com.terrynoya.fight.stateMachine.StateController;
	import com.terrynoya.fight.stateMachine.StateMachine;
	import com.terrynoya.fight.stateMachine.triggers.Abs;
	import com.terrynoya.fight.util.Misc;
	import com.terrynoya.geom.MVector2D;
	
	import flash.geom.Point;
	
	public class HitDef extends StateController
	{
		private var _groundAniType:int;
		private var _groundHitTime:int;
		private var _groundVelocity:MVector2D;
		private var _groundGuardVelocity:MVector2D;
		private var _groundSlideTime:int;
		
		private var _airAniType:int;
		private var _attackEffect:int;
		private var _airAttackEffect:int;
		private var _airHitAniType:int;
		private var _airHitVelocity:MVector2D;
		private var _airGuardVelocity:MVector2D;
		
		private var _hitPause:Point;
		private var _hitTime:int;
		
		private var _fall:Boolean;
		
		private var _yAccel:Number;
		
		private var _hitAnimType:int;
		
		private var _fallHitAnimType:int;
		private var _fallVelocityY:Number;
		
		private var _guardDistance:Number;
		private var _guardHitTime:int;
		private var _guardSlideTime:int;
		
		private var _p1statenumber:int;
		private var _p2statenumber:int;
		
		private var _hitSparkAnimationId:int;
		private var _sparkPos:MVector2D;
		
		public function HitDef(name:String)
		{
			super(name);
		}
		
		public function get groundHitTime():int
		{
			return _groundHitTime;
		}

		public function set groundHitTime(value:int):void
		{
			_groundHitTime = value;
		}
		
		override public function build():void
		{
			this._hitPause = this.getPauseShakeTime();
			this._hitTime = int(this.getAttribute("ground.hittime"));
			this._hitAnimType = Misc.getHitAnimType(this.getAttribute("animtype"));
			this._airHitAniType = this.hasAttribute("air.animtype") ? Misc.getHitAnimType(this.getAttribute("air.animtype")) : this._hitAnimType;
			if(this._airHitAniType == HitAnimationType.Up)
			{
				this._fallHitAnimType = HitAnimationType.Up;
			}
			if(this.hasAttribute("guard.dist"))
			{
				this._guardDistance = this.getAttributeAsInt("guard.dist");
			}
			this._attackEffect = this.getHitAniType();
			this._airAttackEffect = this.getHitAniType();
//			this._groundVelocity = this.getGroundVelocity();
			this._airHitVelocity = this.getAirHitVelocity();
			this._fall = int(this.getAttribute("fall")) == 1 ? true : false;
			this._yAccel = this.getAttributeAsNumber("yaccel",3.5);
			this._fallVelocityY = this.getAttributeAsNumber("fall.yvelocity",-4.5);
			this._guardHitTime = this.getAttributeAsInt("guard.hittime",0);
			this._groundSlideTime = this.getAttributeAsInt("ground.slidetime");
			this._guardSlideTime = this.getAttributeAsInt("guard.slidetime");
			this._airGuardVelocity = Misc.getVelocity(this.getAttribute("airguard.velocity"));
			this._groundGuardVelocity = Misc.getVelocity(this.getAttribute("guard.velocity"));
			
			this._p1statenumber = this.getAttributeAsInt("p1stateno");
			this._p2statenumber = this.getAttributeAsInt("p2stateno");
			
			this._sparkPos = Misc.getVelocity(this.getAttribute("sparkxy"));
			this._hitSparkAnimationId = this.getSparkNo(this.getAttribute("sparkno"));
		}

		override public function run(character:Character):void
		{
			trace("HitDef trigger");
			this.setDifinition(character,character.offensiveInfo.hitDef);
			character.offensiveInfo.activeHitDef = true;
			character.offensiveInfo.hitPauseTime = 0;
		}
		
		private function getPauseShakeTime():Point
		{
			var val:String = this.getAttribute("pausetime");
			var valArr:Array = val.split(",");
			var attackPause:int = valArr[0];
			var oppentShake:int = valArr[1];
			return new Point(attackPause,oppentShake);
		}
		
		private function getGroundVelocity(chara:Character):MVector2D
		{
//			var str:String = this.getAttribute("ground.velocity");
//			return Misc.getVelocity(str);
			var vec:MVector2D = this.transAttrToVec("ground.velocity",chara);
			return vec;
		}
		
		private function getAirHitVelocity():MVector2D
		{
			var str:String = this.getAttribute("air.velocity");
			return Misc.getVelocity(str);
		}
		
		private function getHitAniType():int
		{
			switch(this.getAttribute("ground.type"))
			{
				case "High":
				{
					return AttackEffect.High;
					break;
				}
				case "Low":
				{
					return AttackEffect.Low;
					break;
				}
				case "Trip":
				{
					return AttackEffect.Trip;
					break;
				}
				default:
				{
					return AttackEffect.None;
					break;
				}
			}
		}
		
		protected function setDifinition(chara:Character,hitDef:HitDefinition):void
		{
			hitDef.pauseTime = this._hitPause.x;
			hitDef.shakeTime = this._hitPause.y;
			if(isNaN(this._guardDistance))
			{
				hitDef.guardDistance = chara.playerConst.attackDistance;
			}
			else
			{
				hitDef.guardDistance = this._guardDistance;
			}
			
			hitDef.groundHitTime = this.groundHitTime;
			hitDef.groundAttackEffect = this._attackEffect;
//			hitDef.groundVelocity = this._groundVelocity;
			hitDef.groundVelocity = this.getGroundVelocity(chara);
			hitDef.groundHitTime = this._hitTime;
			hitDef.airAttackEffect = this._airAttackEffect;
			hitDef.fallVelocityY = this._fallVelocityY;
			hitDef.fall = this._fall;
			hitDef.yAcceleration = this._yAccel;
			hitDef.airAnimationType = this._airHitAniType;
			hitDef.groundAnimationType = this._hitAnimType;
			hitDef.airHitVelocity = this._airHitVelocity;
			hitDef.fallAnimationType = this._fallHitAnimType;
			hitDef.groundSlideTime = this._groundSlideTime;
			if(this.hasAttribute("guard.hittime"))
			{
				hitDef.guardHitTime = this._guardHitTime;
			}
			else
			{
				hitDef.guardHitTime = hitDef.groundHitTime;
			}
			
			if(this.hasAttribute("guard.slidetime"))
			{
				hitDef.guardSlideTime = this._guardSlideTime;
			}
			else
			{
				hitDef.guardSlideTime = hitDef.groundSlideTime;
			}
			
			if(this.hasAttribute("guard.velocity"))
			{
				hitDef.groundGuardVelocity = this._groundGuardVelocity;
			}
			else
			{
				hitDef.groundGuardVelocity = new MVector2D(hitDef.groundVelocity.x,0);
			}
			
			if(this.hasAttribute("airguard.velocity"))
			{
				hitDef.airGuardVelocity = this._airGuardVelocity;
			}
			else
			{
				hitDef.airGuardVelocity = hitDef.airHitVelocity.mutiply(new MVector2D(1.5,0.5));	
			}
			
			if(this.hasAttribute("p1stateno"))
			{
				hitDef.p1NewState = this._p1statenumber;	
			}
			if(this.hasAttribute("p2stateno"))
			{
				hitDef.p2NewState = this._p2statenumber;	
			}
						
			hitDef.p2UseP1State = this.getAttributeAsBoolean("p2getp1state",true);
			
			hitDef.sparkPosition = this._sparkPos;
			hitDef.sparkAnimation = this._hitSparkAnimationId;
			hitDef.targetId = this.getAttributeAsInt("ID");
		}
		
		private function getSparkNo(value:String):int
		{
			var rlt:int = int(value.substr(1));
			return rlt;	
		}
	}
}